package com.bieyang.anjia.ui.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bieyang.anjia.R;
import com.bieyang.anjia.net.NetManager;
import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.bieyang.anjia.utils.Show;

public class AcFindPassword extends BaseActivity implements OnClickListener {
	private EditText etCode;
	EditText usertxt;
	public ProgressDialog pd;
	Context con;
	String phone;
	// String username;

	Button btnYanzheng;
	public boolean runTime;
	int timeNum = 60;

	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Constants.GET_FINDPASSCODE_OK:
				Show.toast(con, R.string.register_code_ok);

				btnYanzheng.setEnabled(false);
				runTime = true;
				new Thread() {
					public void run() {
						while (timeNum >= 0) {
							try {
								Thread.sleep(1000);
								handler.sendEmptyMessage(6060);
								timeNum--;
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					};
				}.start();

				break;
			case Constants.GET_FINDPASSCODE_FAIL:

				Show.toast(con, R.string.register_code_fail);
				break;
			case Constants.YANZHENG_FAIL:
				Show.toast(con, R.string.yanzhengError);
				break;
			case Constants.YANZHENG_OK: // next step ok
				Intent intent = new Intent(con, AcModifyPassword.class);
				intent.putExtra("phone", phone);
				startActivity(intent);
				finish();
				break;

			case 6060:
				if (timeNum > 0) {
					btnYanzheng.setText(String.valueOf(timeNum) + "s后再获取");
				} else {
					btnYanzheng.setEnabled(true);
					btnYanzheng.setText(R.string.get_code);
				}

				break;
			default:
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_password);
		con = AcFindPassword.this;
		// String name = getIntent().getStringExtra("name");
		usertxt = (EditText) findViewById(R.id.MyPhone);
		etCode = (EditText) findViewById(R.id.etCode);
		if (getIntent().getBooleanExtra("isLogin", false)) {

			usertxt.setEnabled(true);
		} else {
			String name = SharedPrefsUtil.getStringValue(App.mContext, "phone",
					"");
			if (!"".equals(name))
				usertxt.setText(name);
		}

		findViewById(R.id.btn_findpass).setOnClickListener(this);
		findViewById(R.id.btnBack).setOnClickListener(this);
		findViewById(R.id.btn_checekmail).setOnClickListener(this); // next step

		btnYanzheng = (Button) findViewById(R.id.btnYanzheng);
		btnYanzheng.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		// username = usertxt.getText().toString();
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.btnYanzheng:
			timeNum = 60;
			phone = usertxt.getText().toString().trim();
			Log.d("username", phone + "b:" + checkUser(phone));

			if (!checkUser(phone))
				return;
			btnYanzheng.setEnabled(false);
			getRegisterCode(phone);
			break;

		case R.id.btn_checekmail: // next step
			String code = etCode.getText().toString().trim();
			// phone = usertxt.getText().toString().trim();

			if ("".equals(code)) {
				Show.toast(this, R.string.input_not_empty);
				return;
			}

			stepYanzheng(code);

			break;
		case R.id.btn_findpass:
			phone = usertxt.getText().toString();

			break;
		}
	}

	public boolean checkUser(String name) {

		if (!("".equals(name))) {
			if (11 != name.length()) {
				Show.toast(this, R.string.input_right_phone);
				return false;
			} else {
				return true;
			}

		} else {
			Show.toast(this, R.string.input_not_empty);
			return false;
		}

	}

	/**
	 * 获取验证码
	 * */
	void getRegisterCode(String name) {
		App.netManager.getFindpasswordCode(name, handler);

	}

	/**
	 * next step
	 * */
	void stepYanzheng(String code) {

		App.netManager.stepYanzheng(code, handler);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		} else
			//
			return super.onKeyDown(keyCode, event);
	}

}
